package com.tz.crm.controller.customer;

import com.tz.crm.entity.User;
import com.tz.crm.entity.VisitRecord;
import com.tz.crm.entity.VisitType;
import com.tz.crm.entity.enumeration.Level;
import com.tz.crm.entity.enumeration.Status;
import com.tz.crm.service.UserService;
import com.tz.crm.service.VisitRecordService;
import com.tz.crm.service.VisitTypeService;
import com.tz.crm.utils.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 实现多条件组合查询（其中客户专员和查询时间是两个必选项）;
 * 显示出查询记录;
 */

@Controller
@RequestMapping("/visitRecord")
public class VisitRecordController {

    @Resource
    private VisitRecordService visitRecordService;
    @Resource
    private VisitTypeService visitTypeService;
    @Resource
    private UserService userService;


    @RequestMapping("/initSearch")
    public String getVisitTypes(HttpServletRequest request) {
        //从DataBase获取所有的拜访方式
        List<VisitType> typeList = visitTypeService.getAll();
        request.getSession().setAttribute("typeList", typeList);
        return "visitRecord";
    }


    @RequestMapping("initOwnerList")
    public String getAllUser(HttpServletRequest request) {
        List<User> users = userService.getAll();
        request.setAttribute("users", users);
        return "chooseUser";
    }


    @RequestMapping("/search")
    public String searchRecords(HttpServletRequest request, Pager pager) {

        /**
         * visitType
         */
        List<VisitType> visitTypeList = new ArrayList<>();
        //获取List<VisitType>
        String visitTypeStr = request.getParameter("__multiselect_visitTypeArr");
        if (null != visitTypeStr && visitTypeStr.trim().length() != 0) {
            String[] visitTypeIDs = visitTypeStr.split(":");
            visitTypeList = visitTypeService.get(visitTypeIDs);
            request.setAttribute("visitTypeIDs", Arrays.asList(visitTypeIDs));
        }

        //获取是否为第一次拜访
        String onlyFirstVisitStr = request.getParameter("__checkbox_onlyFirstVisit");
        boolean onlyFirstVisit = false;
        if ("true".equals(onlyFirstVisitStr)) {
            onlyFirstVisit = true;
        }
        request.setAttribute("onlyFirstVisitStr", onlyFirstVisitStr);


        /**
         * Level
         */
        List<Level> levelList = new ArrayList<>();
        //获取 List<Level>
        String levelStr = request.getParameter("__multiselect_levelArr");
        if (null != levelStr && levelStr.trim().length() > 0) {
            //分割
            String[] levels = levelStr.split(":");
            request.setAttribute("levels", Arrays.asList(levels));
            Level level = null;
            //判断获客户等级
            for (String lev : levels) {
                if ("ONE".equals(lev)) {
                    level = Level.ONE;
                } else if ("TWO".equals(lev)) {
                    level = Level.TWO;
                } else if ("THREE".equals(lev)) {
                    level = Level.THREE;
                } else if ("Four".equals(lev)) {
                    level = Level.FOUR;
                } else if ("FIVE".equals(lev)) {
                    level = Level.FIVE;
                }
                levelList.add(level);
            }
        }


        /**
         *Status
         */
        List<Status> statusList = new ArrayList<>();
        //获取状态
        String statusStr = request.getParameter("__multiselect_statusArr");
        //System.out.println("statusStr = " + statusStr);
        if (null != statusStr && statusStr.trim().length() > 0) {
            //分割
            String[] statuss = statusStr.split(":");
            request.setAttribute("statuss", Arrays.asList(statuss));
            Status status = null;
            for (String sta : statuss) {
                //判断
                if ("UNSIGNED".equals(sta)) {
                    status = Status.UNSIGNED;
                } else if ("SEA".equals(sta)) {
                    status = Status.SEA;
                } else if ("SIGNED".equals(sta)) {
                    status = Status.SIGNED;
                } else if ("UNSIGNED_YICHUSHI".equals(sta)) {
                    status = Status.UNSIGNED_YICHUSHI;
                } else if ("UNSIGNED_YIFUSHI".equals(sta)) {
                    status = Status.UNSIGNED_YIFUSHI;
                } else if ("SIGNED_LEAVE".equals(sta)) {
                    status = Status.SIGNED_LEAVE;
                }
                statusList.add(status);
            }


        }

        //获取登录的用户
        User user = (User) request.getSession().getAttribute("user");

        /**
         * owner
         */
        List<User> ownerList = new ArrayList<>();
        //为跳转判断是否显示准备
        String ownersNameStr = request.getParameter("ownersName");
        request.setAttribute("ownersName", ownersNameStr);
        //获取客户专员
        String idsStr = request.getParameter("owners");
        if (null != idsStr && idsStr.trim().length() > 0) {
            String[] ids = idsStr.split(",");
            for (String id : ids) {
                User owner = userService.get(id);
                ownerList.add(owner);
            }
        }


        Pager<VisitRecord> recordPager = visitRecordService.search(onlyFirstVisit, pager, visitTypeList, levelList, ownerList, user, statusList);
        //System.out.println("recordPager = " + recordPager.getList());
        

        request.setAttribute("recordPager", recordPager);
        return "visitRecord";
    }

}
